library(sf)
library(raster)
library(dplyr)
library(mapview)
library(RColorBrewer)
For this demonstration, we are using the cookfarm dataset from the GSIF package. This dataset is a list with 6 somponents storing various measurements that have been done on the farm.
data('cookfarm', package = "GSIF")
names(cookfarm)
## [1] "readings" "profiles" "bdensity" "grids" "weather"
## [6] "proj4string"
The profiles component stores soil profile data as a data.frame. We will turn this into a simple featrure using the sf package:
profiles <- cookfarm$profiles %>%
st_as_sf(coords = c('Easting', 'Northing'), crs = cookfarm$proj4string)
# simple plot using the sf package
plot(profiles)
The grids component is storing gridded data. We will reansform these into a RasterStack using the rasterFromXYZ function. This function requires the x and y columns to be the first column of the data.frame (from the left) – a good opportunity to use the select function from the dplyr package:
grids <- cookfarm$grids %>%
dplyr::select(x, y, DEM, TWI, Cook_fall_ECa, Cook_spr_ECa) %>% # Re-order the columns, and select 4 interesting variables
rasterFromXYZ(crs = cookfarm$proj4string) # The coordinate reference system is stored as one of the component in cookfarm
# simple plot using the raster package
plot(grids)
mapview# couldn't be easier to create a map!
mapview(profiles)
# Tweaking palettes
pal_continuous <- colorRampPalette(brewer.pal(7, "BrBG"))
pal_categorical <- colorRampPalette(brewer.pal(9, "Set1"))
mapview(profiles, zcol = "TAXSUSDA", col.regions = pal_categorical, legend = TRUE)
# Tweaking backgrounds
mapview(profiles, zcol = "BLD", col.regions = pal_continuous, legend = TRUE, map.types = "Esri.WorldImagery")
# Burst to separate soil classes
mapview(profiles, zcol = "TAXSUSDA", col.regions = pal_categorical, legend = TRUE, burst = TRUE)